<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ include file="../session.jsp"%>
<!DOCTYPE html>
<html lang="ko">
  <head>
  	<%@ include file="../head.jsp"%>${alert}
  </head>
  <body>
  	<%@ include file="../menu.jsp"%>
	<div class="container">
		<div class="page-header clearfix">
			<h2 class="pull-left">추천제품 관리</h2>
			<div class="pull-right" style="padding-top: 30px">
				<a href="#" id="opensearch" class="btn btn-info">검색조건 펼치기/접기</a>
				<a href="insertEditerProduct.do" class="btn btn-info">등록하기</a>
			</div>
		</div>
		
		<div id="searchdiv">
			<form id="searchEditerProduct" class="form-horizontal" role="form" method="get" action="editerProduct.do">
				<fieldset>
					<div class="clearfix">
					
						<div class="form-group">
							<label for="firtCategory" class="col-md-2 control-label">카테고리</label>
							<div class="col-md-10">
								<div class="row">
									<div class="col-md-6">
										<select class="form-control" name="firstCategory">
											<option value="default">- 1차카테고리 -</option>
											<c:forEach items="${first}" var="first">
												<option value="${first.idFirstCategory}">${first.firstCategoryText}</option>
											</c:forEach>
										</select>
									</div>
									<div class="col-md-6">
										<select class="form-control" name="secondCategory">
											<option value="default">- 1차를 먼저 선택하여 주세요 -</option>
										</select>
									</div>
								</div>
							</div>
						</div>
						
						<div class="form-group">
							<div class="col-md-offset-2 col-md-10">
								<div class="row">
									<div class="col-md-6">
										<button type="submit" class="btn btn-primary btn-block">검색하기</button>
									</div>
									<div class="col-md-6">
										<button type="reset" class="btn btn-warning btn-block">다시작성</button>
									</div>
								</div>
							</div>
						</div>
					</div>
				</fieldset>
			</form>
			<hr/>
		</div>
		
		<span class="pull-left" style="margin-bottom:10px;">등록 : ${page.totalArticleCount}건 / 활성 : ${display}건 / 비활성 : ${unDisplay}건</span>
		<div class="table-responsive">
			<table class="table table-striped table-hover">
				<thead>
					<tr class="warning row">
						<th class="text-center col-md-1">Top</th>
						<th class="text-center col-md-2">브랜드명</th>
						<th class="text-center col-md-3">카테고리</th>
						<th class="text-center col-md-2">제품명</th>
						<th class="text-center col-md-1">시작일</th>
						<th class="text-center col-md-1">종료일</th>
						<th class="text-center col-md-1">수정</th>
						<th class="text-center col-md-1">
							<c:choose>
								<c:when test="${sort eq 'isDisplay'}">
									<a href="editerProduct.do?order=isUnDisplay${where}">전시여부 ▲</a>
								</c:when>
								<c:otherwise>
									<a href="editerProduct.do?order=isDisplay${where}">전시여부 ▼</a>
								</c:otherwise>
							</c:choose>
						</th>
					</tr>
				</thead>
				<tbody>
					<c:forEach items="${editerProduct}" var="list">
						<tr class="text-center row">
							<td class="col-md-1">
								<c:if test="${list.isTop == 1}">Y</c:if>
								<c:if test="${list.isTop == 0}">N</c:if>
							</td>
							<td class="col-md-2">${list.idBrandValue}</td>
							<td class="col-md-3">${list.idFirstCategoryValue} / ${list.idSecondCategoryValue}</td>
							<td class="col-md-2">${list.idProductValue}</td>
							<td class="col-md-1">${fn:substring(list.start_date, 0, 4)}-${fn:substring(list.start_date, 4, 6)}-${
								fn:substring(list.start_date, 6, 8)}</td>
							<td class="col-md-1">${fn:substring(list.end_date, 0, 4)}-${fn:substring(list.end_date, 4, 6)}-${
								fn:substring(list.end_date, 6, 8)}</td>
							<td class="col-md-1">
								<a href="updateEditerProduct.do?pId=${list.idProduct}&sId=${list.idSecondCategory}&type=EDITER">
									<button type="button" class="btn btn-primary btn-sm">수정</button>
								</a>
							</td>
							<c:choose>
								<c:when test="${list.isDisplay == 1}">
									<td class="col-md-1">Y
										<a href="editerProduct.do?page=${page.nowPage}&pId=${list.idProduct}&sId=${
											list.idSecondCategory}&type=EDITER&isDisplay=${list.isDisplay}${order}${where}">
											<button type="button" class="btn btn-xs btn-defalut unDisplay">삭제</button>
										</a>
									</td>
								</c:when>
								<c:otherwise>
									<td class="col-md-1">N
										<a href="editerProduct.do?page=${page.nowPage}&pId=${list.idProduct}&sId=${
											list.idSecondCategory}&type=EDITER&isDisplay=${list.isDisplay}${order}${where}">
											<button type="button" class="btn btn-xs btn-primary display">승인</button>
										</a>
									</td>
								</c:otherwise>
							</c:choose>
						</tr>
					</c:forEach>
				</tbody>
			</table>
		</div>
		
		<div class="text-center">
			<ul class="pagination">

				<!-- 이전 그룹 -->
				<c:choose>
					<c:when test="${page.startPage > page.pageGroupCount}">
						<li><a href="editerProduct.do?page=${page.startPage-1}${where}${order}">&laquo;</a></li>
					</c:when>
					<c:otherwise>
						<li class="disabled"><a href="#">&laquo;</a></li>
					</c:otherwise>
				</c:choose>
				
				<!-- 페이지 번호 -->
				<c:forEach begin="${page.startPage}" end="${page.endPage}" step="1" var="now">
					<c:choose>
						<c:when test="${page.nowPage == now}">
							<li class="active"><a href="#">${now}</a></li>
						</c:when>
						<c:otherwise>
							<li><a href="editerProduct.do?page=${now}${where}${order}">${now}</a></li>
						</c:otherwise>
					</c:choose>
				</c:forEach>

				<!-- 다음 그룹 -->
				<c:choose>
					<c:when test="${page.endPage < page.lastGroupFirstPage}">
						<li><a href="editerProduct.do?page=${page.endPage+1}${where}${order}">&raquo;</a></li>
					</c:when>
					<c:otherwise>
						<li class="disabled"><a href="#">&raquo;</a></li>
					</c:otherwise>
				</c:choose>

			</ul>
		</div>

	</div>
	<!-- Javascript -->
	<script src="assets/js/jquery.js"></script>
	<script src="assets/js/bootstrap.min.js"></script>
	<script type="text/javascript">
		$(function() {
			$("select[name='firstCategory']").change(function() {
				$("select[name='secondCategory']").empty();
				
				var first = $(this).find("option:selected").val();
				
				$("select[name='thirdCategory'] option").remove();
				$("select[name='thirdCategory']").append("<option value='default'>- 2차를 먼저 선택하여 주세요 -</option>");
				
				$.ajax({
					url: "ajax/first.jsp",
					data: "first=" + first,
					dataType: "xml",
					timeout: 30000,
					cache: false,
					async: false,
					success: function(req) {
						$(req).find("item").each(function() {
							var idSecondCategory = $(this).find("idSecondCategory").text();
							var secondCategoryText = $(this).find("secondCategoryText").text();
							
							var option = $("<option>").html(secondCategoryText);
							option.attr({'value' : idSecondCategory});
							
							$("select[name='secondCategory']").append(option);
						});
					},
					error: function(xhr, status, error) {
						alert("status: " + status + " / error-code: " + xhr.status + " " + error);
					}
				});
			});
		});
	</script>
	<script>
		$(function() {
			$("#searchdiv").hide();
			
			$("#opensearch").click(function() {
				$("#searchdiv").toggle();
				return false;
			});
			
			$(".unDisplay").click(function() {
				if (!confirm("정말 비활성처리 하시겠습니까?")) {
					return false;
				}
			});
			
			$(".display").click(function() {
				if (!confirm("정말 승인처리 하시겠습니까?")) {
					return false;
				}
			});
		});
	</script>
  </body>
</html>